// 全局过滤器
Vue.filter('dateFormat',function(d){
    // 创建日期对象
    let date=new Date(d);

    // 获取对象的年，月，日
    let year=date.getFullYear();

    // 小于10的月份和日期前面补0
    let month=date.getMonth()+1 <10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
    let day=date.getDay()<10 ? "0"+(date.getDay()) : date.getDay();

    return year+"-"+month+"-"+day;

});

let zx=new Vue({
    el:'#information',
    data:{
        user:''
    },
    created(){
        let user=sessionStorage.getItem("user");
        if(user!=null){
            this.user=JSON.parse(sessionStorage.getItem("user"));
            $("#pic2").attr("src","images/"+this.user.image);
        }else{
            $("#pic2").attr("src","images/userImg.png");
        }
    },
    methods:{
        save:function(){
            if(this.user==""){
                return;
            }
          
            let formData={
                userId:this.user.userId,
                userName:$("#userName").val(),
                sex:$("input[name='genber']:checked").val(),
                phone:$("#phone").val(),
                image:this.user.image

            }
            $.ajax({
                url:'http://localhost:8080/cinema/api/UserUpdatePersonServlet.do',
                type:'put',
                dataType:'json',
                data:JSON.stringify(formData),     // 把JavaScript对象，转换为JSON字符串
                success:function(dto){
                    if(dto.code==200){
                        // 重新设置会话存储数据
                        let user={
                            userId:zx.user.userId,
                            userName:$("#userName").val(),
                            password:zx.user.password,
                            phone:$("#phone").val(),
                            sex:$("input[name='genber']:checked").val(),
                            image:zx.user.image,
                            registerTime:zx.user.registerTime,
                            status:zx.user.status
                        }
                        sessionStorage.setItem('user',JSON.stringify(user));

                        layer.msg(dto.value,{icon:1});
                    }else{
                        layer.msg(dto.msg,{icon: 5});
                    }
                }
            });
        }
        
    }
});